<?
$combate = $manager->readObj($combateId, 'combates');
$atacante = $pers;
$defensor = $manager->readObj($combate->getValue('defensor'), 'actores');

$ata_initiative = $combate->getValue('ata_initiative');
$def_initiative = $combate->getValue('def_initiative');

if ($ata_initiative > $def_initiative) {
	var_dump($action);
	if ($action[0] == 'invocar') {
		if ($action[1] == 'acid arrow') $atacante->acidArrow($defensor, $manager, $html);
	}
	if ($action[0] == 'ingerir') $atacante->ingerirObj($action[1], $manager, $html);
	if ($action[0] == 'atacar') $atacante->atacar($defensor, $combate, $manager, $html);

	$thtml .= $html->get('com_result');
	if ($defensor->getValue("hp") >= -10 || ($defensor->getValue("nonLethalDamage") <= $defensor->getValue("hp"))) {
		$defensor->atacar($atacante, $combate, $manager, $html);
		$thtml .= $html->get('com_result');
	}
} else {	
	$defensor->atacar($atacante, $combate, $manager, $html);
	$thtml .= $html->get('com_result');
	if ($atacante->getValue("hp") >= -10 || ($atacante->getValue("nonLethalDamage") <= $atacante->getValue("hp"))) {
		
		var_dump($action);
		if ($action[0] == 'invocar') {
			if ($action[1] == 'acid arrow') $atacante->acidArrow($defensor, $manager, $html);
		}
		if ($action[0] == 'ingerir') $atacante->ingerirObj($action[1], $manager, $html);
		if ($action[0] == 'atacar') $atacante->atacar($defensor, $combate, $manager, $html);
			
		$thtml .= $html->get('com_result');
	}
}

// CHECK DEATHS
if ($defensor->getValue("hp") <= -10
	|| ($defensor->getValue("nonLethalDamage") > $defensor->getValue("hp"))
	|| $atacante->getValue('hp') <= -10
	|| ($atacante->getValue("nonLethalDamage") > $atacante->getValue("hp"))) {
	$atacante->setValue('combateId', null);
	$defensor->setValue('combateId', null);
	
	if ($defensor->getValue("nonLethalDamage") > $defensor->getValue("hp")) {
		$new_xp = $atacante->getValue('xp') + ceil((rand(1, 20) + $defensor->getValue("hp_max") / 5));
		$atacante->setValue('xp', $new_xp);
		$thtml .= $html->mixArray($defensor->toArray(), 'defensor_helpless_combate');
	} elseif ($atacante->getValue("nonLethalDamage") > $atacante->getValue("hp")) {
		$new_xp = $defensor->getValue('xp') + ceil((rand(1, 20) + $atacante->getValue("hp_max") / 5));
		$defensor->setValue('xp', $new_xp);
		$thtml .= $html->mixArray($atacante->toArray(), 'defensor_helpless_combate');
	}

	if ($defensor->getValue("hp") <= -10) {
		$new_xp = $atacante->getValue('xp') + ceil((rand(1, 20) + $defensor->getValue("hp_max") / 3));
		$atacante->setValue('xp', $new_xp);
		$lost_xp = ceil($defensor->getValue('xp') - $defensor->getValue('xp') / 3);
		$defensor->setValue('xp', $lost_xp);
		$defensor->setValue('area_nome', null);
		$areaActual->removeActor($defensor);
		$manager->writeObj($areaActual, 'nome', 'areas');
		$thtml .= $html->mixArray($defensor->toArray(), 'defensor_morreu_combate');
	} elseif ($atacante->getValue("hp") <= -10) {
		$new_xp = $defensor->getValue('xp') + ceil((rand(1, 20) + $atacante->getValue("hp_max") / 3));
		$defensor->setValue('xp', $new_xp);
		$lost_xp = ceil($atacante->getValue('xp') - $atacante->getValue('xp') / 3);
		$defensor->setValue('xp', $lost_xp);
		$atacante->setValue('area_nome', null);
		$areaActual->removeActor($atacante);
		$manager->writeObj($areaActual, 'nome', 'areas');
		$thtml .= $html->mixArray($atacante->toArray(), 'defensor_morreu_combate');
	}
	
}
$html->set('com_result', $thtml);

$manager->writeObj($defensor, 'nome', 'actores');
$manager->writeObj($atacante, 'nome', 'actores');
$pers = $atacante;
?>